package com.example.demo.com.java8.lambda;


import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chengfei5
 * @date 2018/8/20
 */

public class LambdaTest {
    private List<Apple> appleList = Arrays.asList(
            new Apple(155,"green"),
            new Apple(130,"red"),
            new Apple(120,"green"),
            new Apple(159,"green"),
            new Apple(160,"red"),
            new Apple(110,"yellow"),
            new Apple(170,"red"),
            new Apple(180,"green")
    );

    /**
     * for循环筛选绿色苹果
     */
    @Test
    public void forFilterGreenAppleTest(){
        filterGreenApple(appleList).forEach(System.out::println);
    }

    /**
     * for循环筛选重量大于150克苹果
     */
    @Test
    public void forFilterHeavyAppleTest(){
        filterHeavyApple(appleList).forEach(System.out::println);
    }
    /**
     * 优化方式一，策略模式筛选绿苹果测试
     */
    @Test
    public void predicateFilterAppleTest(){
        predicateFilterApple(appleList,new FilterColorApple()).forEach(System.out::println);
        System.out.println();
        predicateFilterApple(appleList,new FilterWeightApple()).forEach(System.out::println);
    }
    /**
     * 优化方式二，策略模式匿名内部类筛选绿苹果测试
     */
    @Test
    public void predicateFilterAppleTest2(){
        predicateFilterApple(appleList, new ApplePredicate<Apple>() {
            @Override
            public boolean test(Apple apple) {
                return "green".equals(apple.getColor());
            }
        }).forEach(System.out::println);

        System.out.println();

        predicateFilterApple(appleList, new ApplePredicate<Apple>() {
            @Override
            public boolean test(Apple apple) {
                return apple.getWeight() > 150;
            }
        }).forEach(System.out::println);
    }

    /**
     * 优化方式三，lambda表达式
     */
    @Test
    public void predicateFilterAppleTest3(){
        predicateFilterApple(appleList,(apple)->"green".equals(apple.getColor()) &&  apple.getWeight() > 150).forEach(System.out::println);
        System.out.println();
        predicateFilterApple(appleList,(apple -> apple.getWeight() > 150) ).forEach(System.out::println);
    }

    /**
     * 优化方式四，stream流
     */
    @Test
    public void predicateFilterAppleTest4(){
        appleList.stream()
                .filter(apple -> "green".equals(apple.getColor()))
                .collect(Collectors.toList())
                .forEach(System.out::println);
        System.out.println();
        appleList.stream().filter(apple -> apple.getWeight()>150)
                .map(Apple::getColor)
                .limit(2)
                .collect(Collectors.toList())
                .forEach(System.out::println);
    }
    /**
     * 策略模式筛选苹果
     * @param appleList
     * @param predicate
     * @return
     */
    private List<Apple> predicateFilterApple(List<Apple> appleList,ApplePredicate<Apple> predicate){
        List<Apple> apps = new ArrayList<>();
        for (Apple apple:appleList){
            if(predicate.test(apple)){
                apps.add(apple);
            }
        }
        return apps;
    }
    /**
     * 通过for循环筛选绿色苹果
     * @param appleList
     * @return
     */
    private List<Apple> filterGreenApple(List<Apple> appleList){
        List<Apple> greenAppleList = new ArrayList<>();
        for (Apple apple:appleList){
            if("green".equals(apple.getColor())){
                greenAppleList.add(apple);
            }
        }
        return greenAppleList;
    }

    /**
     * 通过for循环筛选重量大于150的苹果
     * @param appleList
     * @return
     */
    private List<Apple> filterHeavyApple(List<Apple> appleList){
        List<Apple> greenAppleList = new ArrayList<>();
        for (Apple apple:appleList){
            if(apple.getWeight() > 150){
                greenAppleList.add(apple);
            }
        }
        return greenAppleList;
    }


}
